package com.eece496.Server;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eece496.Database.Assignments;
import com.eece496.Database.COGS;
import com.eece496.Database.COGSAssignments;
import com.eece496.Database.Classroom;
import com.eece496.Database.Course;
import com.eece496.Database.Enrollment;
import com.eece496.Database.Grades;
import com.eece496.Database.Student;
import com.eece496.Database.StudentGroups;
import com.eece496.Database.TAEnrollment;
import com.eece496.Database.TASaveData;
import com.eece496.Database.TAduties;
import com.eece496.Database.TeachingAssistant;
import com.eece496.Helpers.CourseInfo;

/**
 * Servlet implementation class ProfCourseServlet
 */
public class ProfCourseServlet extends MasterServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ProfCourseServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		String ButtonClicked = request.getParameter("button");
		String firstname = request.getParameter("firstname");
		String lastname = request.getParameter("lastname");
		
		if (ButtonClicked.equals("Add New Course")) {

			String isEmpty = "false";
			request.setAttribute("isEmpty", isEmpty);
			request.setAttribute("firstname", firstname);
			request.setAttribute("lastname", lastname);
			request.getRequestDispatcher("/Professor/Professor-add-course.jsp")
					.forward(request, response);
		} else if (ButtonClicked.equals("Delete Course")) {
			String String1 = request.getParameter("courseNumber");
			String courseNumber = String1.substring(0, 8);
			String sectionNumber = String1.substring(9, 12);
			Connect();
			// Delete all Student Groups

			try {	
				DeleteCourseInformation(courseNumber, sectionNumber);
				String isEmpty = "false";
				ArrayList<CourseInfo> info = new ArrayList<CourseInfo>();
				Course CourseDB = new Course();
				CourseDB.Setup(connection);
				info = CourseDB.GetAllCourseInfo();

				request.setAttribute("firstname", firstname);
				request.setAttribute("lastname", lastname);
				request.setAttribute("CourseInfo", info);
				request.setAttribute("isEmpty", isEmpty);

				if (info.size() == 0) {
					isEmpty.equals("true");
					System.out.println(isEmpty);
				}

				request.getRequestDispatcher("/Professor/Professor-Portal.jsp")
						.forward(request, response);
			}

			catch (SQLException e) {
				// // TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else if (ButtonClicked.equals("View Course")) {
			String String1 = request.getParameter("courseNumber");
			String courseNumber = String1.substring(0, 8);
			String sectionNumber = String1.substring(9, 12);
			request.setAttribute("firstname", firstname);
			request.setAttribute("lastname", lastname);
			request.setAttribute("courseNumber", courseNumber);
			request.setAttribute("sectionNumber", sectionNumber);
			request.getRequestDispatcher("/Professor/Professor-Course-Portal.jsp")
					.forward(request, response);
		}
		
		else if(ButtonClicked.equals("Clear Database"))
		{
			//Delete ALL FROM THE DATABASE (EXCEPT FOR THE ADMIN)
			try {
				DeleteAll();
			
			//Variable for determining if the student has an empty course list
			String isEmpty = "false";
			
			//Get course List
			ArrayList<CourseInfo> info = new ArrayList<CourseInfo>();
			Course CourseDB = new Course();
			CourseDB.Setup(connection);
			
			info = CourseDB.GetAllCourseInfo();
			
			//check if the prof has an empty courselist
			if(info.size() == 0)
			{
				isEmpty.equals("true");
				System.out.println(isEmpty);
			}
			
			//Forward to JSP
			request.setAttribute("firstname", firstname);		
			request.setAttribute("lastname", lastname);	
			request.setAttribute("CourseInfo", info);		
			request.setAttribute("isEmpty", isEmpty);	
			
			request.getRequestDispatcher(
					"/Professor/Professor-Portal.jsp").forward(
							request, response);
			}
			catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
	
	private void DeleteCourseInformation(String courseNumber, String sectionNumber) throws SQLException
	{
		
		// Delete all the Grades
		Grades GradesDB = new Grades();
		GradesDB.Setup(connection);
		GradesDB.DeleteCourseGrades(courseNumber, sectionNumber);

		//Delete all TA Save Data
		TASaveData TASaveDataDB = new TASaveData();
		TASaveDataDB.Setup(connection);
		TASaveDataDB.DeleteCourseSaveData(courseNumber,sectionNumber);
		
		//Delete all TA Duties
		TAduties TADutiesDB = new TAduties();
		TADutiesDB.Setup(connection);
		TADutiesDB.DeleteCourseDuties(courseNumber, sectionNumber);
		
		// Delete all the Assignments
		COGSAssignments AssignmentDB = new COGSAssignments();
		AssignmentDB.Setup(connection);
		AssignmentDB.DeleteCourseAssignments(courseNumber,
				sectionNumber);
		
		//Delete COGS
		COGS COGSDB = new COGS();
		COGSDB.Setup(connection);
		COGSDB.DeleteCourseCOGS(courseNumber, sectionNumber);
		
		//Delete StudentGroups
		StudentGroups SgroupsDB = new StudentGroups();
		SgroupsDB.Setup(connection);
		SgroupsDB.DeleteCourseGroups(courseNumber, sectionNumber);
		
		
		//Delete Classrooms
		Classroom ClassroomDB = new Classroom();
		ClassroomDB.Setup(connection);
		ClassroomDB.DeleteCourseClassroom(courseNumber, sectionNumber);
		

		// Delete all the Enrolled TAs and Enrolled Students
		Enrollment EnrollmentStudentDB = new Enrollment();
		EnrollmentStudentDB.Setup(connection);
		EnrollmentStudentDB.DeleteCourseEnrollment(courseNumber,
				sectionNumber);

		TAEnrollment EnrollmentTADB = new TAEnrollment();
		EnrollmentTADB.Setup(connection);
		EnrollmentTADB.DeleteCourseEnrollment(courseNumber,
				sectionNumber);

		Course CourseDB = new Course();
		CourseDB.Setup(connection);
		CourseDB.Delete(courseNumber, sectionNumber);
	}
	
	private void DeleteAll() throws SQLException
	{
		// Delete all the Grades
		Grades GradesDB = new Grades();
		GradesDB.Setup(connection);
		GradesDB.DeleteAll();
		
		//Delete all TA Save Data
		TASaveData TASaveDataDB = new TASaveData();
		TASaveDataDB.Setup(connection);
		TASaveDataDB.DeleteAll();
		
		//Delete all TA Duties
		TAduties TADutiesDB = new TAduties();
		TADutiesDB.Setup(connection);
		TADutiesDB.DeleteAll();
		
		// Delete all the Assignments
		COGSAssignments AssignmentDB = new COGSAssignments();
		AssignmentDB.Setup(connection);
		AssignmentDB.DeleteAll();
		
		//Delete COGS
		COGS COGSDB = new COGS();
		COGSDB.Setup(connection);
		COGSDB.DeleteAll();
		
		//Delete StudentGroups
		StudentGroups SgroupsDB = new StudentGroups();
		SgroupsDB.Setup(connection);
		SgroupsDB.DeleteAll();
		
		//Delete Classrooms
		Classroom ClassroomDB = new Classroom();
		ClassroomDB.Setup(connection);
		ClassroomDB.DeleteAll();
		

		// Delete all the Enrolled TAs and Enrolled Students
		Enrollment EnrollmentStudentDB = new Enrollment();
		EnrollmentStudentDB.Setup(connection);
		EnrollmentStudentDB.DeleteAll();
		TAEnrollment EnrollmentTADB = new TAEnrollment();
		EnrollmentTADB.Setup(connection);
		EnrollmentTADB.DeleteAll();

		Course CourseDB = new Course();
		CourseDB.Setup(connection);
		CourseDB.DeleteAll();
		
		//Delete all Students and TAs in the Database
		Student StudentDB = new Student();
		StudentDB.Setup(connection);
		StudentDB.DeleteAll();
		
		TeachingAssistant TADB = new TeachingAssistant();
		TADB.Setup(connection);
		TADB.DeleteAll();
		
	}
}
